<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html>
<html lang="en">

<head>

    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>SB Admin 2 - Dashboard</title>

    <!-- Custom fonts for this template-->
    <link href="../vendor/fontawesome-free/css/all.min.css" rel="stylesheet" type="text/css">
    <link
        href="https://fonts.googleapis.com/css?family=Nunito:200,200i,300,300i,400,400i,600,600i,700,700i,800,800i,900,900i"
        rel="stylesheet">

    <!-- Custom styles for this template-->
    <link href="../css/sb-admin-2.min.css" rel="stylesheet">

</head>

<body id="page-top">
<%@ include file="../components/checkLoginState.jsp"%>
<jsp:useBean id="getTestList" class="system.getTestList" scope="page"/>
<jsp:useBean id="getQuestions" class="system.getQuestions" scope="page"/>
<jsp:useBean id="createAnswer" class="system.createAnswer" scope="page"/>
<%!
String testid,studentid;
int question_count;
String[][] question_list;
boolean insert=true;

String single_answer;
String multiple_answer_A,multiple_answer_B,multiple_answer_C,multiple_answer_D;
String subject_answer;
%>
<%
testid=request.getParameter("testid");
studentid=(String)session.getAttribute("userid");
question_count=getQuestions.getCount(testid);
question_list=new String[question_count][3];
question_list=getQuestions.getQuestionsList(testid);
createAnswer.setStudentId(studentid);
createAnswer.setTestId(testid);

int type_tag=0;
for(int i=0;i<question_count;i++){
    type_tag=Integer.parseInt(question_list[i][1].trim());
    if(type_tag==1){
        single_answer=request.getParameter("question_"+question_list[i][0]);
        insert=createAnswer.createSingle(question_list[i][0],single_answer);
    }else if(type_tag==2){
        multiple_answer_A=request.getParameter("question_A_"+question_list[i][0]);
        multiple_answer_B=request.getParameter("question_B_"+question_list[i][0]);
        multiple_answer_C=request.getParameter("question_C_"+question_list[i][0]);
        multiple_answer_D=request.getParameter("question_D_"+question_list[i][0]);
        insert=createAnswer.createMultiple(question_list[i][0],multiple_answer_A,multiple_answer_B,multiple_answer_C,multiple_answer_D);
    }else if(type_tag==3){
        subject_answer=request.getParameter("question_"+question_list[i][0]);
        byte b[]=subject_answer.getBytes("ISO-8859-1");
        subject_answer=new String(b);
        insert=createAnswer.createSubject(question_list[i][0],subject_answer);
    }
}
insert=createAnswer.finishTest();
if(insert){response.sendRedirect("finishSuccessfully.jsp?testid="+testid);};
%>
</body>

</html>